#!/usr/bin/perl

use strict;
use warnings;

# tell Perl where to find MozComics package
BEGIN { push @INC, "/home/mozcomic/scrapers/"; }

use MozComics::JSON;
use MozComics::MySQL;

use LWP::Simple;
use DateTime;
use DateTime::Format::Strptime;
use HTML::Entities qw(decode_entities);

my $guid = 'a81f87df-7943-4f88-abf1-2bf238a667c6';
#my $comic = MozComics::JSON->new($guid);
my $comic = MozComics::MySQL->new($guid);
my $comicName = $comic->getName();

my $lastStripRow = $comic->getLastStrip();
my $lastStrip = 0;

my $dateParser = new DateTime::Format::Strptime(
	pattern => '%B %d, %Y',
	locale  => 'en_US'
);

# scrape information from a page
sub scrapePage {
	my $page = shift(@_) or die "$comicName: page not passed to scrapePage";
	my $data = {};
	my @scraped;

	my $idRegex = qr/URL="http:\/\/www\.smbc-comics\.com\/index\.php\?db=comics&id=(\d+)"/;
	@scraped = $comic->scrapeUnique($page, $idRegex);
	my $id = $scraped[0];
	$data->{url} = "http://www.smbc-comics.com/index.php?db=comics&id=$id";

	# scrape 'strip' value
	my $stripRegex = qr/\<b\>News:\<\/b\>\<br\>\<a name="blog"\> \<b\>(.+?)\<\/b\>/;
	@scraped = $comic->scrapeUnique($page, $stripRegex);
	my $dateString = $scraped[0];

	# Handle special case where date isn't found
	if ($id eq '2069') {
		$dateString = "November 22, 2010";
	}

	my $date = $dateParser->parse_datetime($dateString)
	           or die "$comicName: could not parse date ($dateString)";
	my $strip = $date->epoch();

	if($strip <= $lastStrip) {
		$strip = $lastStrip; #die "$comicName: $title is out of order?";
	}
	$strip += 1000;
	$lastStrip = $strip;
	$data->{strip} = $strip;

	# scrape 'title' value
	$data->{title} = $dateString;

	# scrape 'image' value
	if($id eq '436') {
		$data->{image} = 'http://www.smbc-comics.com/comics/20060302';
		$data->{extra} = '';
	}
	else {
		my %mainImagesHash = ();
		my %hiddenImagesHash = ();

		while($page =~ /\<img src='http:\/\/(www|zs1)\.smbc-comics\.com\/comics\/(.+?)\.(gif|jpg|png)\s*'\>/gs) {

			my $foundExtension = $3;
			my $foundImage = "http://www.smbc-comics.com/comics/$2.$foundExtension";
			if($foundImage =~ /after\.$foundExtension/) {
				$hiddenImagesHash{$foundImage} = 1;
			}
			else {
				$mainImagesHash{$foundImage} = 1;
			}
		}

		my @mainImages = keys(%mainImagesHash);
		my $mainImagesSize = @mainImages;
		if($mainImagesSize == 0) {
			die "$comicName: expected to find a main image";
		}
		if($mainImagesSize > 1) {
			die "$comicName: found more than one main image";
		}
		$data->{image} = $mainImages[0];

		my @hiddenImages = keys(%hiddenImagesHash);
		my $hiddenImagesSize = @hiddenImages;
		$data->{extra} = '';

		if($page =~ /http:\/\/(www|zs1)\.smbc-comics\.com\/images\/secretvoteybutton\.gif/) {

			if($hiddenImagesSize == 0) {
				die "$comicName: expected to find a hidden image";
			}
			if($hiddenImagesSize > 1) {
				die "$comicName: found more than one hidden image";
			}

			$data->{extra} = '{"hiddenImage":"'.$hiddenImages[0].'"}';
		}
		elsif($hiddenImagesSize != 0) {
			die "$comicName: did not expect to find hidden images";
		}
	}

	return $data;
}

my $stripPage = get($lastStripRow->{url})
                or die "$comicName: last page url not found";
my $lastStripData = scrapePage($stripPage);
$comic->assertStripsEqual($lastStripData, $lastStripRow);

$lastStrip = $lastStripRow->{strip};

while($stripPage =~ /\<area shape="rect" coords="351,21,425,87"\s+href="\/(.*?)#comic"\s*\>/s) {

	my $url = "http://www.smbc-comics.com/$1";

	$stripPage = get($url) or die "$comicName: next strip page not found";

	#print "$url\n";
	$comic->addStrip(scrapePage($stripPage));
}

$comic->commit();

